package com.site_assembly.apiImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.service.SiteApiService;
import com.service.form.SiteForm;
import com.site_assembly.common.ValidationUtils;
import com.site_assembly.service.ISiteService;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;


@RestController
@Slf4j
@Api(tags = "子站点端接口", description = "提供子站点相关的 Rest API")
public class SiteApiImpl implements SiteApiService {

    @Resource
    private ISiteService siteService;

    @Override
    @ApiOperation("新建子站点")
    public Result addSite(@Valid SiteForm siteForm) {

        log.info("子站点信息{}", siteForm);

        ValidationUtils.validate(siteForm);
        Integer num = siteService.addSite(siteForm);

        return num > 0 ? Result.success(20000, "新建成功") : Result.fail(20000, "新建失败");
    }

    @Override
    @ApiOperation("根据站点状态分页查询（null则查询全部）子站点")
    public Result findSiteByPageAndOperatingState(@ApiParam(name = "operatingState", value = "子站点状态") String operatingState,
                                                  @ApiParam(name = "current", value = "当前页", example = "1") Integer current,
                                                  @ApiParam(name = "pageSize", value = "页容量", example = "1") Integer pageSize) {

        log.info("子站点状态{}，当前页{}，页容量{}", operatingState, current, pageSize);

        IPage sites = siteService.findSiteByPageAndOperatingState(operatingState, current, pageSize);
        return Result.success(sites);
    }

    @Override
    @ApiOperation("根据子站点编号审核修改子站点状态（存续/注销）")
    @ApiResponses({@ApiResponse(code = 20000, message = "修改成功"), @ApiResponse(code = 40000, message = "修改失败")})
    public Result setSiteOfOperatingStateBySiteNumber(@ApiParam(name = "operatingState", value = "子站点状态") String operatingState,
                                                      @ApiParam(name = "siteNumber", value = "子站点编号") String siteNumber) {

        log.info("子站点状态{}，子站点编号{}", operatingState, siteNumber);
        Integer num = siteService.setSiteOfOperatingStateBySiteNumber(operatingState, siteNumber);
        return num > 0 ? Result.success() : Result.fail();
    }

}
